﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sherwood.SignOn.Server.Models.Repositories;
using Sherwood.Mvc;
using Sherwood.SignOn.Server.Models.Services;
using Sherwood.SignOn.Server.Content;

namespace Sherwood.SignOn.Server.Models.ViewModels
{
    public class VerifyViewModel : ViewModelBase
    {
        public string UserAccountId { get; set; }
        public string RequestedUrl { get; set; }
        public string VerificationCode { get; set; }
        public string MessageToUser { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }

        public string RedirectUrl
        {
            get
            {
                if (!string.IsNullOrEmpty(RequestedUrl))
                    return "/SignIn/?ReturnUrl=" + HttpUtility.UrlEncode(RequestedUrl);
                else
                    return "/SignIn/";
            }
        }

        /// <summary>
        /// Verifies the user based on the value of the UserAccountId and VerificationCode.
        /// If the user is successfully verified, the verification status is changed to "Verified" 
        /// and the RequestedUrl variable is set (the user can be automatically redirected to this Url).
        /// If the user is not verified, the MessageToUser property will contain a message to the user.
        /// </summary>
        /// <param name="state"></param>
        /// <returns>True if user is verified, False if user could not be verified.</returns>
        public bool VerifyUser(ModelStateDictionary state)
        {
            MessageToUser = Translations.Verify_Failure.Parameterize(this, Config.Settings);
            if (!string.IsNullOrEmpty(UserAccountId) && !string.IsNullOrEmpty(VerificationCode))
            {
                IUserAccount account = Data.UserAccounts.GetUserAccount(new Guid(UserAccountId));
                if (account != null)
                {
                    SetCulture(account.CultureName1, account.CultureName2, account.CultureName3);
                    if (account.VerificationStatus != VerificationStatus.Unverified)
                    {
                        MessageToUser = Translations.Verify_Already_Verified.Parameterize(this, account, Config.Settings);
                        return false;
                    }
                    if (account.ValidateVerificationCode(VerificationCode))
                    {
                        MessageToUser = Translations.Verify_Success.Parameterize(this, account, Config.Settings);
                        RequestedUrl = account.RequestedUrl;

                        account.SetVerificationParameters(VerificationStatus.Verified, null, null);

                        return true;
                    }
                }
                else
                {
                    MessageToUser = Translations.Verify_Failure_User_Not_Found.Parameterize(this, Config.Settings);
                }
            }

            return false;
        }


        public bool VerifyUserProfileModification(ModelStateDictionary ModelState)
        {
            if (string.IsNullOrEmpty(Username))
            {
                ModelState.AddModelError("Username","*");
                return false;
            }
            if (string.IsNullOrEmpty(Password))
            {
                ModelState.AddModelError("Password", "*");
                return false;
            }
            IUserAccount user = Data.UserAccounts.AuthenticateUser(Username, Password);
            if (user == null || !user.Id.Equals(new Guid(UserAccountId)))
            {
                ModelState.AddModelError("",Translations.SignIn_Invalid_Username_Or_Password);
                return false;
            }
            MessageToUser = Translations.EditProfileConfirmation_Verification_Failed_Message.Parameterize(this, Config.Settings);
            if (!string.IsNullOrEmpty(UserAccountId) && !string.IsNullOrEmpty(VerificationCode))
            {

                IUserAccount account = Data.UserAccounts.GetUserAccount(new Guid(UserAccountId));
                if (account != null)
                {
                    SetCulture(account.CultureName1, account.CultureName2, account.CultureName3);
                    if (account.ValidateProposedEmailAndPasswordChange(VerificationCode))
                    {
                        RequestedUrl = account.RequestedUrl;
                        MessageToUser = Translations.EditProfileConfirmation_Verification_Successful.Parameterize(this, account, Config.Settings);
                        account.SetVerificationParameters(VerificationStatus.Verified, null, null);
                        return true;
                    }
                }
            }
            return false;
        }
    }
}
